package ru.spies.profileClasses;

import ru.spies.dbwork.DBWork;
import ru.spies.entities.Role;
import ru.spies.entities.Siteuser;


public class UserInfo {

	private static UserInfo statThis=null;
	private UserPrivileges privileges;
	private Siteuser me;
	
	private static final String ROLE_GUEST="guest";
	private static final String ROLE_ADMIN="admin";
	private static final String ROLE_AGENT="agent";
	private static final String ROLE_RESIDENT="resident";
	
	private static final String NAME_GUEST="Гость";
		
	private UserInfo() {
		Role role=getGuestRole();
		initPrivileges(role);
		me = new Siteuser();
		me.setAgent(null);
		me.setRole(role);
		me.setId(null);
		me.setPassword(null);
		me.setUsername(NAME_GUEST);
	}
	
	private UserInfo(Siteuser user){
		this.me=user;
		initPrivileges(user.getRole());
	}
	
	public static UserInfo getInstance(){
		if (statThis==null){
			statThis= new UserInfo();
		}
		return statThis;
	}
	
	public void login(Siteuser user){
		statThis= new UserInfo(user);
	}
	
	public void logout(){
		statThis = new UserInfo();
	}
	
	private void initPrivileges(Role role){
		if (role.getName().equals(ROLE_GUEST)){
			createGuest();
			return;
		}
		if (role.getName().equals(ROLE_AGENT)){
			createNormalAgent();
			return;
		}
		if (role.getName().equals(ROLE_RESIDENT)){
			createResident();
			return;
		}
		createAdmin();
		return;
	}
	
	private void createAdmin(){
		privileges=UserPrivileges.createUserWithAllPrivileges();
	}
	
	private void createGuest(){
		privileges=UserPrivileges.createUserWithoutPrivileges();
	}
	
	private void createResident(){
		privileges=UserPrivileges.createUserWithResidentPrivilages();
	}
	
	private void createNormalAgent(){
		privileges=UserPrivileges.createUserWithStandartPrivilages();
	}

	private Role getGuestRole(){
		Role guest = new Role();
		guest.setParamForFindQuery(ROLE_GUEST);
		guest=(Role) DBWork.getInstance().findObjectFromTable(guest);
		return guest;
	}
	
	public Role getNormalRole(){
		Role agent = new Role();
		agent.setParamForFindQuery(ROLE_AGENT);
		agent=(Role) DBWork.getInstance().findObjectFromTable(agent);
		return agent;
	}
	
	public UserPrivileges getPrivileges(){
		return privileges;
	}
	
	public Siteuser getMe(){
		return me;
	}
	
	public String getMyInfo(){
		/*
		 *  <p>Вы вошли как:</p>
			<p class="userInfo"></p>
			<p>Группа:</p>
			<p class="userInfo"></p>
		 */
		return "<p>Вы вошли как:</p><p class=\"userInfo\">"+me.getUsername()+"</p><p>Группа:</p><p class=\"userInfo\">"+getTrueRole(me.getRole().getName())+"</p>";
	}
	
	private String getTrueRole(String roleName){
		if (roleName.equals(ROLE_GUEST)) return "Гость";
		if (roleName.equals(ROLE_AGENT)) return "Агент";
		if (roleName.equals(ROLE_RESIDENT)) return "Глава отдела";
		if (roleName.equals(ROLE_ADMIN)) return "Администратор";
		return null;
	}
	
	public boolean isRoleGuest(){
		if (me.getRole().getName().equals(ROLE_GUEST)) return true;
		return false;
	}
	
	public boolean isRoleResident(){
		if (me.getRole().getName().equals(ROLE_RESIDENT)) return true;
		return false;
	}
	
}
